<?php

require_once ('IUserService.php');
require_once ('BaseService.php');
require_once 'dao/UserDao.php';
require_once 'util/ServiceUtil.php';
require_once 'util/StringUtil.php';

class UserService extends BaseService implements IUserService {
	
	public static $NAME = "UserService";
	protected $userDao;
	
	public function UserService()
	{
		$this->initializeDB();
		$this->userDao = new UserDao();
	}
	
	public function create(UserDao $object) 
	{
		if($object)
		{
			try
			{
				$this->sql = "INSERT INTO user(name,password) VALUES('$object->name','md5($object->password)')";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
	
	public function delete($id) 
	{
		if($id)
		{
			try
			{
				$this->sql = "DELETE * FROM user WHERE id = '$id'";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
	
	public function read(UserDao $object) 
	{
		$username = strtolower(trim($object->username));
		$password = md5(strtolower(trim($object->password)));
		$id		  = strtolower(trim($object->id));
		if($username==NULL)
		{
			$this->sql = "SELECT * FROM user";
			if($id!=NULL)
			{
				$this->sql = "SELECT * FROM user WHERE id='$id'";
			}
			try
			{
				$this->query = mysql_query($this->sql,$this->myDb);
				$this->numRows = mysql_num_rows($this->query);
				
				for($i=0;$i<$this->numRows;$i++)
	 			{
	 				$row = mysql_fetch_object($this->query);
	 				
	 				$this->userDao->id = $row->id;
					$this->userDao->username = $row->username;
					$this->userDao->password = $row->password;
					$this->userDao->is_admin = $row->is_admin;
					$this->userDao->time = $row->time;
						
					$this->result[] = $this->userDao;
	 			}
				return $this->result;
			}catch(Exception $exc)
			{
				return $exc;
			}
		}else
		{
			try
			{
				$this->sql = "SELECT * FROM user WHERE username='$username' AND password='$password'";
				$this->query = mysql_query($this->sql,$this->myDb);
				$this->numRows = mysql_num_rows($this->query);
				if($this->numRows>=1)
				{
					return ServiceUtil::getSuccessResult(ServiceUtil::SUCCESS,(__CLASS__).("::").(__FUNCTION__));
				}else
				{
					return ServiceUtil::getFailureResult(ServiceUtil::FAILURE,(__CLASS__).("::").(__FUNCTION__));
				}
				
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		
		return NULL;
	}
	
	public function update(UserDao $object) 
	{
		if($object)
		{
			try
			{
				$this->sql = "UPDATE TABLE user SET username='$object->username',
							password='$object->password',is_amdin='$object->is_amdin'";
				$this->query = mysql_query($this->sql,$this->myDb);
				return $this->checkQueryResult(mysql_affected_rows($this->myDb));
			}catch(Exception $exc)
			{
				return $exc;
			}
		}
		return NULL;
	}
}

?>